// var util = require("../../util/util.js");
const appInstance = getApp();
var url = appInstance.globalData.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [],
    videoList: [],
    classifyList: [],
    indicatorDots: false,
    autoplay: true,
    circular: true,
    interval: 5000,
    duration: 1000,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    loading: false,
    currentPage: 1,
    currentIndex: 0,
    previousMargin: 68,
    nextMargin: 68
  },


  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    this.ONslideList();
    this.OnClassifyList();
    this.Hotbroadcast();
  },

  /* 这里实现控制中间凸显图片的样式 */
  handleChange: function (e) {
    this.setData({
      currentIndex: e.detail.current
    })
  },
  //轮播图列表
  ONslideList() {
    var that = this;
    appInstance.request({
      url: '/banner/list',
      success(res) {
        console.log(res)
        that.setData({
          imgUrls: res.data.data
        })
      }
    })
  },
  //分类列表
  OnClassifyList() {
    var that = this;
    appInstance.request({
      url: '/category/list',
      success(res) {
        // console.log(res)
        that.setData({
          classifyList: res.data.data
        })
      }
    })
  },

  //首页正在热播
  Hotbroadcast() {
    // var that = this;
    // appInstance.request({
    //   url: '/video/list',
    //   data: {
    //     type: 1
    //   },
    //   success(res) {
    //     console.log(res)
    //     var val = res.data.data
    //     that.setData({
    //       videoList: res.data.data
    //     })
    //   }
    // })
    this.onReachBottom();
  },


  //轮播跳转到h5
  swipclick(e) {
    // console.log(e)
    var id = e.currentTarget.id;
    // console.log(id)
    wx.navigateTo({
      url: '../test/test?id=' + id,
    })

  },
  //跳转search页面
  tapName(e) {
    console.log(e)
    wx.navigateTo({
      url: '../search/search'
    })
  },

  //跳转分类视频
  classifyTap(e) {
    console.log(e)
    var id = e.currentTarget.id;
    console.log(id)
    wx.navigateTo({
      url: './classify/classify?id=' + id
    })
  },
  //跳转到视频详情
  videtDetail(e) {
    // console.log(e.target.id)
    var Id = e.target.id;
    wx.navigateTo({
      url: './classify/videoDetail/videoDetail?id=' + Id,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // 显示顶部刷新图标
    // wx.showNavigationBarLoading()
    this.Hotbroadcast()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    that.setData({
      loading: true
    })
    wx.request({
      url: url + '/video/list',
      data: {
        page: that.data.currentPage,
        size: 4
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      dataType: 'json',
      success(res) {
        console.log(res.data.data)
        that.setData({
          loading: false
        })
        if (res.data.data.length != 0) {
          that.setData({
            videoList: that.data.videoList.concat(res.data.data),
            currentPage: that.data.currentPage + 1,
          })
        }
      }
    })
 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})